/**
 * Copyright (c) 2010 - 2012, Sina Inc. All rights reserved.
 * @fileoverview Sina  编辑图片
 * @author bowen liu | liubowen082@163.com
 * @version 1.1 | 2013-4-13
 * @example
 * 
 */


$require('form.cFormselect');
$require('mod.cPrompt');
$require('mod.cAlert');
LJS.define('cpt.edProPic',function(ljs){
    var edProPic = {
       data : null,
       init: function(){
           var that = this;
           
           that.select = new ljs.form.cFormSelect({
               el: $('#typeSelect')[0]
           });
           $(that).on('changeData', function(evt,data){
           //数据改变
             // 变图片上的点，变瓣品列表的序号
		     $('#editPicNum').html(data.pros.length)
             $.each(data.pros,function(i,a){
                 $('#editPicBox span').eq(i).html(i+1);
                 $('#editPicProList p').eq(i).find('em').html(i+1);
				 $('#editPicProList p').eq(i).find('a').attr('id',i);
             }) 
           })
        
        this.addEvent();
       },
       setSelectCon : function(){
       	this.select.setSelected(0)
       	return this.select.getValue()
       },
       addEvent:function(){
           var that = this;
           $('#typeSelect').on('change', function(){
		   	if(!that.data) return;
               that.data.space = that.select.getValue();
			   //console.log(that.select.getValue())
			   //console.log(that.data)
               $(that).trigger('changeData',that.data)
           });
           
           $('#editPicBox').on('click','img',function(evt){
               //console.log($('#editCreatePro').attr('editting'))
			   if($('#editCreatePro').attr('editting')!='true')
			   return
               if (!that.data||that.data.pros.length >= 5) {
				   ljs.mod.cAlert({txt:'最多可添加5个产品'})
                   return
               }
               
               var x = evt.pageX - $(this).offset().left;
               var y = evt.pageY - $(this).offset().top;
               var _l = ljs.mod.cPrompt({
                       txt: '<ul>\
                    <li class="fillTxt"><div class="tit" style="width:60px">产品Id：</div><div class="inputTxt"><input name="" type="text" t" node_type="saveProInput"/></div></li>\
                </ul>',
                       fun: function(){
                           var val = $('[node_type=saveProInput]', _l.win).val();
                           if ($.trim(val) !== '') {
                               
                               $.ajax({
                                  type: "GET",
                                  //url: "checkPro.js",
								  url:"/cases/ajax_product",
                                  dataType: "json",
                                  data:{id:val},
								  cache:false,
                                  success:function(json){
                                      if(json.status=='100000'){
                                          _l.del();
                                       var opt = {
                                           name: json.content.name,
										   id:json.content.id,
                                           left: x,
                                           top: y
                                       };
                                       that.data.pros.push(opt);
									   //console.log(opt)
                                       $(that).trigger('changeData', that.data)
                                       $('#editPicNum').html(that.data.pros.length);
                                       // 创建标签
                                       that.createTip(opt, that.data.pros.length)
                                      }else if(json.status=='100003'){
					                	window.location.href = json.content.url;
					                }else{
                                      	
                                      	ljs.mod.cAlert({txt:json.msg})
                                      }
                                  }
                                });
                        }
                    }
                });
				
				$(_l).on('onDel',function(){
					$('#editCreatePro').attr('editting','false')
				})
            })
           // 删除产品
           $('#editPicProList').on('click','a',function(){
		   	    var t = this;
				var _l = ljs.mod.cPrompt({
					txt : '确认删除此项',
					fun : function(){
						var id = parseInt($(t).attr('id'));						
						that.data.pros.splice(id,1);
						that.delTip(id);
						setTimeout(function(){
											$(that).trigger('changeData',that.data)		
								},200)
						_l.del();
					}
				})
			
			
			
				
				})
           // 添加产品
           $('#editCreatePro').on('click',function(evt){
               $(this).attr('editting','true')
               evt.stopPropagation()
           })
           $(window).on('click',function(){
               $('#editCreatePro').attr('editting','false')
           })
		   // 编辑上一个或下一个
		   $('#playBtn').on('click','[node_type="playPro"]',function(){
					$(that).trigger('playNext','pro')													 
		   })
		   $('#playBtn').on('click','[node_type="playNext"]',function(){
					$(that).trigger('playNext','next')													 
		   })
           
       },
	   createTip : function(op,index){
		   
		   var str ='<span class="dian" style="left:'+(op.left-10)+'px;top:'+(op.top-10)+'px">'+index+'</span>'
		   $('#editPicBox').append(str);
		   var txt = '<p><span class="tit"><em>'+index+'</em>.'+op.name+'：</span><span class="btnDel"><a href="javascript:void(0)" id="'+(index-1)+'">删除</a></span></p>'
		   $('#editPicProList').append(txt)
		   },
	   delTip : function(index){
		   $('#editPicBox span').eq(index).remove();
		   $('#editPicProList p').eq(index).remove()
		   },
       setData : function(data,index){
		   var that = this;
           this.data = data;
           $('#editPicName').html('图片'+index+":")
           $('#editPicBox').html('<img src="'+data.url+'" src_big="'+data.url+'" style="width:100%,height:100%"></img>')
           this.select.setSelectedByValue(data.space);
           $('#editPicNum').html(data.pros.length);
		   $('#editPicProList').html('')
		   $.each(data.pros,function(i,a){
						that.createTip(a,i+1)
						 })
       },
       delData : function(){
		   if(!this.data) return
           this.data = null;
           $('#editPicName').html('');
           $('#editPicBox img ,#editPicBox span').remove();
           $('#editPicNum').html('0')
           $('#editPicProList').html('')
       }
        
        
    }
    
    
    
    
    
    return edProPic;





});
